package com.bytech.gateway.entity;

import java.io.Serializable;

import org.springframework.http.HttpStatus;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Author zhao
 * @Create 2021/7/27 10:12
 */
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class GatewayResponse<T> implements Serializable {

    @JSONField(name = "code")
    private Integer code;

    @JSONField(name = "success")
    private boolean success;

    @JSONField(name = "msg")
    private String msg;

    @JSONField(name = "data")
    private T data;

    public static <T> GatewayResponseBuilder<T> success(String msg) {
        return GatewayResponse.<T>builder().success(true).code(HttpStatus.OK.value()).msg(msg);
    }

    public static <T> GatewayResponseBuilder<T> failure(String msg) {
        return GatewayResponse.<T>builder().success(false).code(HttpStatus.INTERNAL_SERVER_ERROR.value()).msg(msg);
    }

    public static <T> GatewayResponseBuilder<T> failure(String msg, HttpStatus status) {
        return GatewayResponse.<T>builder().success(false).code(status.value()).msg(msg);
    }
}
